<?php
namespace Boxes\Controller;
use  Common\Controller\BoxesController;

class IndexController extends BoxesController {

    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    /**
     * 是否显示
     */
    public function isshow(){

        $appid = I('post.appid','','htmlspecialchars');

//        if ($appid=='wxb0449164f7e74d21'){
//            $show = array(
//                'show'=>'0',
//                'url'=>'http://oss.beikg.cn/cai2.jpg',
//            );
//            $this->returnData($show);
//        }

        if ($appid!="wxb0449164f7e74d21"){
            $show = array(
                'show'=>'1',
            );
            $this->returnData($show);
        }

        $show = array(
            'show'=>'1',
        );
        $this->returnData($show);
    }

    /**
     * 获取头部banner
     */
    public function getbanner(){

        $banner = M('boxes_headapp')->where(array('headisshow'=>'1'))->order('sort asc')->select();
        foreach ($banner as $key=>$value){

            $banner[$key]['headimg'] = $this->url.$banner[$key]['headimg'];

        }

        $this->returnData($banner);

    }


    /**
     * 获取中间展示
     */
    public function getcentre(){

        $banner = M('boxes_app')->where(array('isshow'=>'1'))->order('sort asc')->select();


        foreach ($banner as $key=>$value){

            $banner[$key]['logo'] = $this->url.$banner[$key]['logo'];

        }

        $this->returnData($banner);

    }

    /**
     * 获取底部展示
     */
    public function getbottom(){

        $banner = M('boxes_bomttomapp')->where(array('bottomshow'=>'1'))->find();

        $banner['bottomimg'] = $this->url.$banner['bottomimg'];

        $this->returnData($banner);

    }

    /**
     * 存放模板消息
     */
    public function send()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $touser = I('post.touser','','htmlspecialchars');
        $page = I('post.page','','htmlspecialchars');
        $form_id = I('post.form_id','','htmlspecialchars');
        $form_type = I('post.form_type','','htmlspecialchars');
        $secret = I('post.secret','','htmlspecialchars');

        $template_id = I('post.template_id','','htmlspecialchars');

        if ($appid==""||$touser==""||$page==""||$form_id==""||$form_type==""||$secret==""||$template_id==""){
            $this->returnData('','参数错误','401');
        }

        $date = date('Y-m-d');
        $insert = array(
            'appid'=>$appid,
            'touser'=>$touser,
            'template_id'=>$template_id,
            'page'=>$page,
            'form_id'=>$form_id,
            'form_type'=>$form_type?$form_type:1,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>$secret,
            'createtime'=>$date,
        );
        $where['touser'] = $touser;
        $where['template_id'] = $template_id;
        $where['createtime'] = $date;
        $where['status'] = '-1';

        $ishave = M('boxes_template_send')->field('wxopen_template_send_id')->where($where)->find();

        if ($ishave){
            M('boxes_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
        }else{
            M('boxes_template_send')->data($insert)->add();
        }

        $this->returnData('成功','200');

    }

    /**
     * 获取跳转盒子的参数
     */
    public function getapp(){

        $app = M('jump_app')->field('appid,url,envVersion')->find();

        $this->returnData($app);

    }

    public function getstory()
    {

        $banner = M('boxes_fiction')->where(array('isshow'=>'1'))->order('sort asc')->select();

        foreach ($banner as $key=>$value){

            $banner[$key]['logo'] = $this->url.$banner[$key]['logo'];

        }
        $this->returnData($banner);
    }



}